package com.rc.saas.tenant.controller.tenant;

import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.utils.BeanUtil;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.controller.elec.ElecPileController;
import com.rc.saas.tenant.model.tenant.TenantManufacturer;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.base.BaseSearch;
import com.rc.saas.tenant.service.tenant.TenantManufacturerService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * @author frog
 * @date 2019/12/3
 */
@Controller
@RequestMapping("/tenantManufacturer")
public class TenantManufacturerController {
    private static Logger logger = LogManager.getLogger(ElecPileController.class);

    @Autowired
    private TenantManufacturerService tenantManufacturerService;

    /**
     * 跳转运营商生产信息页面
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String tenantManufacturerPage() {
        return "/templates/tenant/manufacturer/tenantManufacturer";
    }

    /**
     * 返回运营商生产信息列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listTenantManufacturer", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPile(BaseSearch baseSearch) {
        PageInfo <TenantManufacturer> pageInfo = tenantManufacturerService.searchByCondition(baseSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }


    /**
     * 跳转编辑运营商生产信息页面
     *
     * @return
     */
    @RequestMapping(value = "/editTenantManufacturer/{manufacturerId}", method = RequestMethod.GET)
    public String editTenantManufacturer(@PathVariable("manufacturerId") Long manufacturerId, Model model) {
        TenantManufacturer tenantManufacturer = tenantManufacturerService.selectByPrimaryKey(manufacturerId);
        model.addAttribute("tenantManufacturer", tenantManufacturer);
        return "/templates/tenant/manufacturer/editTenantManufacturer";
    }

    /**
     * 跳转增运营商生产信息页面
     *
     * @return
     */
    @RequestMapping(value = "/addTenantManufacturer", method = RequestMethod.GET)
    public String addTenantManufacturer() {
        return "/templates/tenant/manufacturer/addTenantManufacturer";
    }

    /**
     * 保存编辑运营商生产信息
     *
     * @return
     */
    @RequestMapping(value = "/saveOrUpdateTenantManufacturer", method = RequestMethod.POST)
    @ResponseBody
    public TResponse saveOrUpdateTenantManufacturer(TenantManufacturer tenantManufacturer) {
        TenantUser user = ShiroUtils.getSessionUser();
        if (!BeanUtil.isValid(tenantManufacturer.getManufacturerCode(),tenantManufacturer.getManufacturerName(),
                tenantManufacturer.getBrandName())) {
            return TResponse.FAIL("请输入正确的参数");
        }
        try {
            TenantManufacturer manufacturer = tenantManufacturerService.selectByPrimaryKey(tenantManufacturer.getManufacturerId());
            tenantManufacturer.setLastUpdatedBy(user.getLoginName());
            tenantManufacturer.setLastUpdatedTime(new Date());
            tenantManufacturer.setTenantCode(user.getTenantCode());
            if (manufacturer != null) {
                tenantManufacturer.setManufacturerId(manufacturer.getManufacturerId());
                tenantManufacturerService.updateByPrimaryKeySelective(tenantManufacturer);
            } else {
                tenantManufacturerService.insertSelective(tenantManufacturer);
            }
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("保存营商生产信息失败", e);
            return TResponse.FAIL("操作失败");
        }
    }


    /**
     * 删除运营商生产信息
     *
     * @return
     */
    @RequestMapping(value = "/deleteTenantManufacturer", method = RequestMethod.POST)
    @ResponseBody
    public TResponse deleteTenantManufacturer(Long manufacturerId) {
        try {
            tenantManufacturerService.deleteByPrimaryKey(manufacturerId);
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("删除营商生产信息失败", e);
            return TResponse.FAIL("操作失败");
        }
    }

    /**
     * 运营商生产信息的上线或者下线
     */
    @ResponseBody
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    public TResponse updateStatus(String manufacturerId) {
        try {

            return TResponse.SUCCESS();
        } catch (Exception e) {
            return TResponse.FAIL("更新状态失败");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/getTenantManufacturerById", method = RequestMethod.POST)
    public TResponse getTenantManufacturerById(Long manufacturerId) {
        try {
            TenantManufacturer tenantManufacturer = tenantManufacturerService.selectByPrimaryKey(manufacturerId);
            TResponse tResponse = TResponse.SUCCESS();
            tResponse.put(tenantManufacturer);
            return tResponse;
        } catch (Exception e) {
            return TResponse.FAIL();
        }
    }

}
